
// IMPORTS
import MASS.*;                  // MASS Library
import java.util.AbstractMap.*; // for simple key-value pairs
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DayDetection extends Place {
    // CONSTANTS - MASS arguments
    private static final String[] MASSARGS = {"dslab", "dslab-302",
        "machinefile.txt", "12345"};            // connection arguments
    private static final int NPROCESSES = 1;    // number of processes to employ
    private static final int NTHREADS = 8;      // number of threads to employ
    private static final int HANDLE = 0;        // unique Places handle number
    public static final int init_ = 0;           // initialize Places
    public static final int expose_ = 1;
    public static final int collect_ = 2;
    public int[] aInts = {1, 1, 2, 3, 3, 3, 4, 6, 1, 2};
    private Integer myDay;
    private        HashMap<Integer, ArrayList<Integer>> dayMap;
//    private ArrayList<Integer> sameDay;
//    private ArrayList<ArrayList<Integer>> allDays;
//    private HashMap<Integer, ArrayList<Integer>> dayMap;

    public DayDetection(Object interval) {
        
    }

    public Object callMethod(int funcId, Object args) {
        switch (funcId) {
            case init_:
                return init(args);
            case expose_:
                return expose(args);
            case collect_:
                return collect(args);

        }
        return null;
    }

    public Object init(Object args) {
        myDay = aInts[index[0]];


        System.err.println("Day Detedction init called: " + index[0]);
        return (Object) null;
    }
    
    public Integer expose(Object args) {
        System.err.println("Mass Four func 1 expose: node: " + index[0]);
        return myDay;
    }
    
    public Object collect(Object args) {
//        Iterator<ArrayList<Integer>> iter = allDays.iterator();
 
        dayMap = new HashMap<Integer, ArrayList<Integer>>();
        System.err.println("In computing node " + index[0]);
//        if (index[0] == 0) {
            
            
            ArrayList<Integer> dayLoc; 
            System.err.println("Message length: " + inMessages.length);
            boolean foundMyDay = false;
//            for (int i = 0 ; i < inMessages.length; ++i) {
            for (int i = 9 - index[0]; i < 19 - index[0]; ++i) {
                if (inMessages[i] == null) System.err.println("InMessage null at " + i);
                System.err.println("In computing node " + index[0] + ", message at " + i + " reads as an object: " + inMessages[i] + ", and reads as an Integer: " + (Integer)inMessages[i]);
                
                // if a different day, then add a day to outer list
                // also add the day's element address IMPORTANT: ArrayLists need another element
                if (inMessages[i] != myDay) {
                    
                }
                if (dayMap.isEmpty() || !dayMap.containsKey((Integer)inMessages[i])) {
                    dayLoc = new ArrayList<Integer>();
                    dayLoc.add(i - 9 + index[0]);
                    dayMap.put((Integer)inMessages[i], dayLoc);
                }
                else {
                    
                    // if the same day as a day in the outer list, add to that day's inner list
                    // also add the day's element address IMPORTANT: ArrayLists need another element
                    dayLoc = dayMap.get((Integer)inMessages[i]);
                    dayLoc.add(i - 9 + index[0]);
                    dayMap.put((Integer)inMessages[i], dayLoc);
                }
                
            } // next day
            
            
//        } // end if
        return dayMap;
    }
    
    public static void main(String[] args) {
        Object[] results;
        HashMap<Integer, ArrayList<Integer>> dayMapper;


        try {
            MASS.init(MASSARGS, NPROCESSES, NTHREADS);
            Object[] fixer = new Object[10];

            Places days;
//            MassFour test = new MassFour(null);
            days = new Places(HANDLE, "DayDetection", null, 10);
            
            days.callAll(init_, fixer);
            Vector<int[]> neighbor = new Vector();
//            int[] dests = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
            for (int i = -9; i < 10; ++i) {
                int[] dests = {i};
            
                neighbor.add(dests);
            }
            
            
            days.exchangeAll(HANDLE, expose_, neighbor);
            Thread.sleep(1000);
            results = days.callAll(collect_, fixer);
            Object result;
            result = results[9];
            dayMapper = (HashMap<Integer, ArrayList<Integer>>)result;
            
            if (!dayMapper.isEmpty()) {
                System.err.println("First key: " + dayMapper.containsKey(1));
                System.err.println("First value: " + dayMapper.get(1).get(0));
                                
                System.err.println("Keys: " + dayMapper.keySet().toString());
                System.err.println("Values: " + dayMapper.values().toString());
            }
            else {
                System.err.println("DayMapper is empty");
            }


////            Places nine = new Places(1, "MassNine", null, 9);
//            
//                        


       

            MASS.finish();
        } catch (Exception ex) {
            Logger.getLogger(MassMultiPlaces.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
        }
    }
    
}
